﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmTimKiemPhieuKeToan : Form
    {
        private PhieuKeToanManagement phieuketoanMgt = new PhieuKeToanManagement();

        private NhanVien nhanvien;
        public NhanVien NhanVien
        {
            set { this.nhanvien = value; }
            get { return this.nhanvien; }
        }

        public FrmTimKiemPhieuKeToan()
        {
            InitializeComponent();
        }

        private void dgvDanhSachPKT_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            PhieuKeToan phieuketoan = (from p in GetAllPhieuKeToan()
                                       where p.Id == ((PhieuKeToan)dgvDanhSachPKT.Rows[e.RowIndex].DataBoundItem).Id
                                       select p).FirstOrDefault();

            FrmPhieuKeToan frmPhieuKeToan = new FrmPhieuKeToan(phieuketoan);
            this.Hide();
            frmPhieuKeToan.ShowDialog();
            this.Show();
            dgvDanhSachPKT.DataSource = null;
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;

            List<PhieuKeToan> lstPhieuKeToan = (from p in GetAllPhieuKeToan()
                                                where (p.MaSo.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                    p.ChungTuKemTheo.ToLower().Contains(txtTimKiem.Text.ToLower()) ||
                                                    p.NoiDung.ToLower().Contains(txtTimKiem.Text.ToLower())) &&
                                                    p.NgayThang.Date >= dtTuNgay.Date && p.NgayThang.Date <= dtDenNgay.Date
                                                select p).ToList();

            dgvDanhSachPKT.AutoGenerateColumns = false;
            dgvDanhSachPKT.DataSource = lstPhieuKeToan;
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnMoi_Click(object sender, EventArgs e)
        {
            FrmPhieuKeToan frmPhieuKeToan = new FrmPhieuKeToan(this.nhanvien);
            this.Hide();
            frmPhieuKeToan.ShowDialog();
            this.Show();
        }

        public List<PhieuKeToan> GetAllPhieuKeToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = phieuketoanMgt.GetAllPhieuKeToan();
            List<PhieuKeToan> listPhieuKeToan = (from pk in(List<PhieuKeToan>)resultObj.Result
                                                     where pk.IsDelete==0
                                                     select pk).ToList();

            return listPhieuKeToan;
        }
    }
}
